package com.HomeWork;

import java.util.Random;

/**
 * 老婆线程不断的取钱，当余额小于1000时，通知老公线程存钱 用notify方法唤醒老公线程。
 * 老公线程存完一笔钱后通知老婆线程可以取钱了，老婆线程继续取钱。
 */
class wife extends Thread {
    work9_Money money;

    public wife(work9_Money money) {
        this.money = money;
    }

    @Override
    public void run() {
        while (true) {
            double m = new Random().nextInt(1000);
            money.quMoney(m);
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }


        }

    }
}

//老公线程检查余额是否足够，当余额小于1000时，
// 随机存1000块，当余额大于1000时线程wait()
class Man extends Thread {
    work9_Money money;

    public Man(work9_Money money) {
        this.money = money;
    }

    /**
     *
     */
    @Override
    public void run() {
        double m = 5000;
        // double m=new Random().nextInt(1000);
        boolean f = true;
        int i = 0;
        while (f) {
            if (i < 10) {

                money.cunMoney(m);
                try {
                    Thread.sleep(0);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                i++;
            } else {
                System.out.println("老婆省着点花 ，老公挣钱不容易");
                f = false;
            }
        }

    }
}


public class work9 {
    public static void main(String[] args) {
        work9_Money work9Money = new work9_Money(5000);

        Thread t1 = new Thread(new wife(work9Money), "老婆");
        Thread t2 = new Thread(new Man(work9Money), "老公");
        t1.start();
        t2.start();
    }


}
